<?php

namespace Database\Seeders;

use App\Models\Dish;
use App\Models\Category;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class EnhancedDishSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 清空现有菜品数据（先删除外键约束）
        \DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        Dish::truncate();
        \DB::statement('SET FOREIGN_KEY_CHECKS=1;');
        
        // 分类ID映射（根据迁移文件中的创建顺序）
        $categoryMap = [
            'hot' => 1,      // 热菜
            'cold' => 2,     // 凉菜
            'soup' => 3,     // 汤品
            'staple' => 4,   // 主食
            'drink' => 5,    // 饮品
            'dessert' => 6,  // 甜品
            'snack' => 7,    // 小吃
        ];
        
        // 详细菜品数据 - 以东北菜为主
        $dishes = [
            // 东北菜 - 热菜
            [
                'name' => '锅包肉',
                'description' => '东北名菜，里脊肉配糖醋汁，外酥内嫩，酸甜可口',
                'category_id' => $categoryMap['hot'],
                'tags' => ['经典', '酸甜', '东北菜', '猪肉'],
                'flavors' => ['酸甜', '香脆'],
                'cooking_time' => 25,
                'is_featured' => true,
                'sort_order' => 1,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '地三鲜',
                'description' => '东北经典，茄子土豆青椒，家常美味，营养丰富',
                'category_id' => $categoryMap['hot'],
                'tags' => ['经典', '家常', '东北菜', '蔬菜'],
                'flavors' => ['香浓', '鲜美'],
                'cooking_time' => 20,
                'is_featured' => true,
                'sort_order' => 2,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '小鸡炖蘑菇',
                'description' => '东北特色，鸡肉配野生蘑菇，汤鲜肉嫩，滋补养生',
                'category_id' => $categoryMap['hot'],
                'tags' => ['特色', '滋补', '东北菜', '鸡肉'],
                'flavors' => ['鲜美', '香浓'],
                'cooking_time' => 45,
                'is_featured' => true,
                'sort_order' => 3,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '猪肉炖粉条',
                'description' => '东北家常菜，五花肉配粉条，肥而不腻，香浓可口',
                'category_id' => $categoryMap['hot'],
                'tags' => ['家常', '香浓', '东北菜', '猪肉'],
                'flavors' => ['香浓', '鲜美'],
                'cooking_time' => 40,
                'is_featured' => false,
                'sort_order' => 4,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '酸菜白肉',
                'description' => '东北传统，酸菜配白肉，酸香开胃，解腻爽口',
                'category_id' => $categoryMap['hot'],
                'tags' => ['传统', '酸香', '东北菜', '猪肉'],
                'flavors' => ['酸香', '鲜美'],
                'cooking_time' => 35,
                'is_featured' => true,
                'sort_order' => 5,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '溜肉段',
                'description' => '东北经典，里脊肉配青椒，外酥内嫩，香浓可口',
                'category_id' => $categoryMap['hot'],
                'tags' => ['经典', '香浓', '东北菜', '猪肉'],
                'flavors' => ['香浓', '鲜美'],
                'cooking_time' => 20,
                'is_featured' => false,
                'sort_order' => 6,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北乱炖',
                'description' => '东北特色，多种蔬菜肉类一锅炖，营养丰富，味道浓郁',
                'category_id' => $categoryMap['hot'],
                'tags' => ['特色', '营养', '东北菜', '混合'],
                'flavors' => ['香浓', '鲜美'],
                'cooking_time' => 50,
                'is_featured' => true,
                'sort_order' => 7,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '红烧肉',
                'description' => '东北家常，五花肉配冰糖，肥而不腻，香甜可口',
                'category_id' => $categoryMap['hot'],
                'tags' => ['家常', '香甜', '东北菜', '猪肉'],
                'flavors' => ['香甜', '鲜美'],
                'cooking_time' => 60,
                'is_featured' => true,
                'sort_order' => 8,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '糖醋排骨',
                'description' => '东北经典，排骨配糖醋汁，酸甜可口，外酥内嫩',
                'category_id' => $categoryMap['hot'],
                'tags' => ['经典', '酸甜', '东北菜', '排骨'],
                'flavors' => ['酸甜', '香脆'],
                'cooking_time' => 30,
                'is_featured' => false,
                'sort_order' => 9,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北大拉皮',
                'description' => '东北特色凉菜，拉皮配黄瓜丝，爽滑可口，开胃解腻',
                'category_id' => $categoryMap['cold'],
                'tags' => ['特色', '爽滑', '东北菜', '凉菜'],
                'flavors' => ['清爽', '微辣'],
                'cooking_time' => 10,
                'is_featured' => true,
                'sort_order' => 10,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '凉拌黄瓜',
                'description' => '东北家常凉菜，黄瓜配蒜泥，爽脆可口，开胃解腻',
                'category_id' => $categoryMap['cold'],
                'tags' => ['家常', '爽脆', '东北菜', '蔬菜'],
                'flavors' => ['清爽', '微辣'],
                'cooking_time' => 5,
                'is_featured' => false,
                'sort_order' => 11,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北酸菜',
                'description' => '东北传统，酸菜配调料，酸香开胃，解腻爽口',
                'category_id' => $categoryMap['cold'],
                'tags' => ['传统', '酸香', '东北菜', '酸菜'],
                'flavors' => ['酸香', '清爽'],
                'cooking_time' => 8,
                'is_featured' => true,
                'sort_order' => 12,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            
            // 汤品
            [
                'name' => '东北酸菜汤',
                'description' => '东北特色汤品，酸菜配肉丝，酸香开胃，暖胃驱寒',
                'category_id' => $categoryMap['soup'],
                'tags' => ['特色', '酸香', '东北菜', '汤品'],
                'flavors' => ['酸香', '鲜美'],
                'cooking_time' => 20,
                'is_featured' => true,
                'sort_order' => 13,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '冬瓜排骨汤',
                'description' => '东北家常汤品，冬瓜配排骨，清淡鲜美，滋补养生',
                'category_id' => $categoryMap['soup'],
                'tags' => ['家常', '滋补', '东北菜', '汤品'],
                'flavors' => ['清淡', '鲜美'],
                'cooking_time' => 60,
                'is_featured' => false,
                'sort_order' => 14,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '西红柿鸡蛋汤',
                'description' => '东北经典汤品，西红柿配鸡蛋，酸甜可口，营养丰富',
                'category_id' => $categoryMap['soup'],
                'tags' => ['经典', '营养', '东北菜', '汤品'],
                'flavors' => ['酸甜', '清淡'],
                'cooking_time' => 15,
                'is_featured' => false,
                'sort_order' => 15,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            
            // 主食
            [
                'name' => '东北大米饭',
                'description' => '东北优质大米，粒粒分明，软糯香甜，配菜绝佳',
                'category_id' => $categoryMap['staple'],
                'tags' => ['优质', '主食', '东北菜', '米饭'],
                'flavors' => ['清淡', '香甜'],
                'cooking_time' => 30,
                'is_featured' => false,
                'sort_order' => 16,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北饺子',
                'description' => '东北特色主食，猪肉白菜馅，皮薄馅大，口感鲜美',
                'category_id' => $categoryMap['staple'],
                'tags' => ['特色', '主食', '东北菜', '饺子'],
                'flavors' => ['鲜美', '香浓'],
                'cooking_time' => 25,
                'is_featured' => true,
                'sort_order' => 17,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北包子',
                'description' => '东北传统主食，猪肉酸菜馅，皮薄馅大，口感丰富',
                'category_id' => $categoryMap['staple'],
                'tags' => ['传统', '主食', '东北菜', '包子'],
                'flavors' => ['鲜美', '香浓'],
                'cooking_time' => 30,
                'is_featured' => true,
                'sort_order' => 18,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北面条',
                'description' => '东北手工面条，配肉丝蔬菜，汤浓味美，营养丰富',
                'category_id' => $categoryMap['staple'],
                'tags' => ['手工', '主食', '东北菜', '面条'],
                'flavors' => ['香浓', '鲜美'],
                'cooking_time' => 20,
                'is_featured' => false,
                'sort_order' => 19,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            
            // 饮品
            [
                'name' => '东北大麦茶',
                'description' => '东北传统饮品，大麦炒制，清香解腻，暖胃养生',
                'category_id' => $categoryMap['drink'],
                'tags' => ['传统', '清香', '东北菜', '茶类'],
                'flavors' => ['清香', '甘甜'],
                'cooking_time' => 5,
                'is_featured' => true,
                'sort_order' => 20,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北酸梅汤',
                'description' => '东北特色饮品，酸梅配冰糖，酸甜可口，开胃解腻',
                'category_id' => $categoryMap['drink'],
                'tags' => ['特色', '酸甜', '东北菜', '果汁'],
                'flavors' => ['酸甜', '清香'],
                'cooking_time' => 10,
                'is_featured' => true,
                'sort_order' => 21,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北蜂蜜水',
                'description' => '东北天然蜂蜜，温水冲泡，甘甜润燥，养生保健',
                'category_id' => $categoryMap['drink'],
                'tags' => ['天然', '甘甜', '东北菜', '蜂蜜'],
                'flavors' => ['甘甜', '清香'],
                'cooking_time' => 3,
                'is_featured' => false,
                'sort_order' => 22,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            
            // 甜品
            [
                'name' => '东北粘豆包',
                'description' => '东北传统甜品，糯米包红豆，香甜软糯，口感丰富',
                'category_id' => $categoryMap['dessert'],
                'tags' => ['传统', '香甜', '东北菜', '糯米'],
                'flavors' => ['香甜', '软糯'],
                'cooking_time' => 40,
                'is_featured' => true,
                'sort_order' => 23,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北糖葫芦',
                'description' => '东北特色甜品，山楂配糖浆，酸甜可口，外脆内软',
                'category_id' => $categoryMap['dessert'],
                'tags' => ['特色', '酸甜', '东北菜', '山楂'],
                'flavors' => ['酸甜', '香脆'],
                'cooking_time' => 15,
                'is_featured' => true,
                'sort_order' => 24,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北冻梨',
                'description' => '东北传统甜品，冻梨配糖水，清甜爽口，解腻开胃',
                'category_id' => $categoryMap['dessert'],
                'tags' => ['传统', '清甜', '东北菜', '冻梨'],
                'flavors' => ['清甜', '爽口'],
                'cooking_time' => 20,
                'is_featured' => false,
                'sort_order' => 25,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            
            // 小吃
            [
                'name' => '东北烤串',
                'description' => '东北特色小吃，羊肉配调料，香辣可口，口感丰富',
                'category_id' => $categoryMap['snack'],
                'tags' => ['特色', '香辣', '东北菜', '羊肉'],
                'flavors' => ['香辣', '鲜美'],
                'cooking_time' => 20,
                'is_featured' => true,
                'sort_order' => 26,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北炸鸡',
                'description' => '东北特色小吃，鸡肉配调料，外酥内嫩，香辣可口',
                'category_id' => $categoryMap['snack'],
                'tags' => ['特色', '香辣', '东北菜', '鸡肉'],
                'flavors' => ['香辣', '香脆'],
                'cooking_time' => 25,
                'is_featured' => true,
                'sort_order' => 27,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
            [
                'name' => '东北煎饼',
                'description' => '东北传统小吃，煎饼配鸡蛋，外酥内嫩，口感丰富',
                'category_id' => $categoryMap['snack'],
                'tags' => ['传统', '香脆', '东北菜', '煎饼'],
                'flavors' => ['香脆', '鲜美'],
                'cooking_time' => 15,
                'is_featured' => false,
                'sort_order' => 28,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
            ],
        ];
        
        // 为每个菜品添加图片和随机数据
        foreach ($dishes as &$dish) {
            $dish['image'] = 'https://picsum.photos/400/300?random=' . rand(1, 1000);
            $dish['is_available'] = rand(0, 10) <= 8; // 80%概率可用
        }
        
        // 批量插入菜品数据
        foreach ($dishes as $dish) {
            Dish::create($dish);
        }
        
        // 更新分类菜品数量
        $this->updateCategoryDishCounts();
        
        $this->command->info('已创建 ' . count($dishes) . ' 个东北菜菜品数据');
    }
    
    /**
     * 更新分类菜品数量
     */
    private function updateCategoryDishCounts(): void
    {
        $categories = Category::all();
        
        foreach ($categories as $category) {
            $count = Dish::where('category_id', $category->id)->count();
            $category->update(['dish_count' => $count]);
        }
        
        $this->command->info('已更新分类菜品数量统计');
    }
}